package com.liy.supercloud.ssoserver.service.impl;

import com.liy.cloud.system.dao.SysPermissionDao;
import com.liy.cloud.system.entity.SysPermissionEntity;
import com.liy.cloud.system.vo.SysPermissionVO;
import com.liy.framework.common.redis.utils.RedisUtils;
import com.liy.framework.common.utils.Tools;
import com.liy.supercloud.ssoserver.service.UserResourcesService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserResourcesServiceImpl implements UserResourcesService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SysPermissionDao sysPermissionDao;


    public List<Object> getCacheMenu(String userId) {
        return null;
    }

    public List<SysPermissionVO> findMyPermission(String sysId, String userId) {
        return (List)(Tools.StringIsNullOrSpace(sysId) && Tools.StringIsNullOrSpace(userId) ? new ArrayList() : this.sysPermissionDao.selectMyPermission(sysId, userId));
    }

    public List<SysPermissionEntity> findSharePermission(String sysId) {
        return sysPermissionDao.selectSharePermission(sysId);
    }

    public void saveSharePermissionToRedis(String sysId) {
        List<SysPermissionEntity> sharePermissionList = this.findSharePermission((String)null);
        Map<String, Object> permissionMaps = new HashMap();
        Map<String, Object> permissionWebMaps = new HashMap();
        this.permissionListToMap(sharePermissionList, permissionMaps, permissionWebMaps);
        this.redisUtils.hmset("PER:COMM", permissionMaps);
        this.redisUtils.hmset("PER:COMMW", permissionWebMaps);
    }

    private void permissionListToMap(List<SysPermissionEntity> sharePermissionList, Map<String, Object> permissionMaps, Map<String, Object> permissionWebMaps) {
        Iterator var4 = sharePermissionList.iterator();

        while(var4.hasNext()) {
            SysPermissionEntity permissionVO = (SysPermissionEntity)var4.next();
            String[] urls = permissionVO.getUrl().split(",");
            String pre = permissionVO.getSysId().substring(permissionVO.getSysId().indexOf("-") + 1, permissionVO.getSysId().length());
            String[] var8 = urls;
            int var9 = urls.length;

            for(int var10 = 0; var10 < var9; ++var10) {
                String url = var8[var10];
                permissionMaps.put("/" + pre + url, permissionVO.getSysId());
            }

            if (!"N".equals(permissionVO.getCode())) {
                permissionWebMaps.put(permissionVO.getCode().replace(".", "_"), permissionVO.getUrl());
            }
        }

    }

    public void saveUserPermissionToRedis(String userId) {
        Map<String, Object> permissionMaps = new HashMap();
        Map<String, Object> permissionWebMaps = new HashMap();
        List<SysPermissionVO> myPermissionList = this.findMyPermission((String)null, userId);
        Iterator var5 = myPermissionList.iterator();

        while(var5.hasNext()) {
            SysPermissionVO permissionVO = (SysPermissionVO)var5.next();
            String[] urls = permissionVO.getUrl().split(",");
            String pre = permissionVO.getSysId().substring(permissionVO.getSysId().indexOf("-") + 1, permissionVO.getSysId().length());
            String[] var9 = urls;
            int var10 = urls.length;

            for(int var11 = 0; var11 < var10; ++var11) {
                String url = var9[var11];
                permissionMaps.put("/" + pre + url, permissionVO.getModelName() + "." + permissionVO.getName());
            }

            permissionWebMaps.put(permissionVO.getCode().replace(".", "_"), permissionVO.getUrl());
        }

    }

    public void synRolePermissionToRedis(String roleId) {
    }
}
